//
//  CaseManageController.m
//  quickDoctor
//
//  Created by zhaowei on 17/3/9.
//  Copyright © 2017年 SWS. All rights reserved.
//

#import "CaseManageController.h"
#import "AddCaseController.h"
#import "CaseTableController.h"
#import "PhotoTableViewCell.h"
#import "NoPhotoTableViewCell.h"

@interface CaseManageController ()<UITableViewDelegate,UITableViewDataSource,NSCoding>

@property (nonatomic,strong)UITableView *tableView;

//字典
@property (nonatomic,strong)NSDictionary *infoDict;

//数组
@property (nonatomic,strong)NSMutableArray *arrM;
@end
static NSString *noPhotoCell = @"noPhotoCell";
static NSString *photoCell = @"photoCell";
static NSString *emptyCell = @"emptyCell";

@implementation CaseManageController {
    //文件路径
    NSString *_fileName;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
    _arrM = [NSMutableArray array];
    //接收通知
    [[NSNotificationCenter defaultCenter] addObserver:self  selector:@selector(reloadCase:) name:@"caseCommit" object:nil];
    
   //读取数据
    NSString *documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
    
    NSString *fileName = [documentPath stringByAppendingPathComponent:@"arr.xml"];
    NSMutableArray *arr = [NSKeyedUnarchiver unarchiveObjectWithFile:fileName];
    if(arr != nil) {
        _arrM = arr;
    }
    
}
#pragma mark
#pragma mark -归档
- (void)encodeWithCoder:(NSCoder *)aCoder {
    [aCoder encodeObject:_arrM forKey:@"arr"];
}
#pragma mark
#pragma mark -解档
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    if(self = [super  initWithCoder:aDecoder]) {
        
    }
    return self;
}

#pragma mark
#pragma mark -接收到通知,然后刷新tableView
- (void)reloadCase:(NSNotification *)notification {
    //取出字典
    NSDictionary *dict = notification.userInfo;
    _infoDict =dict;
    [_arrM insertObject:dict atIndex:0];
    //保存数据
    [self saveValue];
    
    NSIndexSet *indexSet = [[NSIndexSet alloc] initWithIndex:0];
    [self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationTop];
    
  
    
}

#pragma mark
#pragma mark -保存数据
- (void)saveValue {

    NSString *documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
    
    NSString *fileName = [documentPath stringByAppendingPathComponent:@"arr.xml"];
    _fileName = fileName;
   
    [NSKeyedArchiver archiveRootObject:_arrM toFile:fileName];
    //
}
- (void)setupUI{
    
    self.navigationController.navigationBar.translucent = NO;
    //添加ImageView
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.image = [UIImage imageNamed:@"twoWang"];
    [self.view addSubview:imageView];
    
    //约束
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(0);
        make.leading.trailing.offset(0);
        make.height.mas_equalTo(180);
    }];
    
    //添加button
    UIButton *button = [[UIButton alloc] init];
    [button setTitle:@"添加病例" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    button.backgroundColor = RGBCOLOR(41, 188, 186);
    [self.view addSubview:button];
    
    //为按钮添加点击事件
    [button addTarget:self action:@selector(clickPush) forControlEvents:UIControlEventTouchUpInside];
    
    //约束button
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.offset(-15);
        make.leading.offset(15);
        make.trailing.offset(-15);
        make.height.mas_equalTo(40);
    }];
    
    //添加tableView
    UITableView *tableView = [[UITableView alloc] init];
//    tableView.backgroundColor = [UIColor greenColor];
    _tableView = tableView;
    //隐去中间线
    tableView.separatorStyle = NO;
    [self.view addSubview:tableView];
    
    //留空白
    tableView.tableFooterView = [UIView new];
    
    //设置代理
    tableView.delegate = self;
    tableView.dataSource = self;
    //约束tableView
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(imageView.mas_bottom);
        make.bottom.mas_equalTo(button.mas_top).offset(-10);
        make.leading.trailing.offset(0);
    }];
    
    //注册
    [tableView registerClass:[PhotoTableViewCell class] forCellReuseIdentifier:photoCell];
    [tableView registerClass:[NoPhotoTableViewCell class] forCellReuseIdentifier:noPhotoCell];

    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:emptyCell];
}
#pragma mark
#pragma mark -点击跳转
- (void)clickPush {
    //创建控制器
    AddCaseController *addCnv = [[AddCaseController alloc] init];
    
    [self.navigationController pushViewController:addCnv animated:YES];
    
}

#pragma mark
#pragma mark -tableView的数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    

    return _arrM.count ;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        NSData *data1 = UIImagePNGRepresentation(_arrM[indexPath.row][@"photoImage"]);
            NSData *data2 = UIImagePNGRepresentation([UIImage imageNamed:@"compose_pic_add_highlighted"]);
            if([data1 isEqual:data2]) {
                //如果没有选择相片
                NoPhotoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:noPhotoCell forIndexPath:indexPath];
                cell.noPhotoArrM =_arrM;
                
                //取出数组中的字典
                NSDictionary *dict = _arrM[indexPath.row];
                cell.dataLabel.text = dict[@"dateTime"];
                cell.detailTypeLabel.text = dict[@"typeText"];
                if([dict[@"divisionText"] isEqualToString:@"请选择"]) {
                    cell.baseDetailTypeLabel.text = @"无";
                }else {
                cell.baseDetailTypeLabel.text = dict[@"divisionText"];
                }
                if([dict[@"textViewText"] isEqualToString:@"请输入病症描述"]) {
                    
                cell.textViewLabel.text = @"";
                }else {
                    cell.textViewLabel.text =dict[@"textViewText"];
                }
                
                
                return cell;
            }else {
        
                //如果选择了相片
                 PhotoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:photoCell forIndexPath:indexPath];
        cell.photoArrM = _arrM;
        //取出数组中的字典
        NSDictionary *dict = _arrM[indexPath.row];
        cell.dataLabel.text = dict[@"dateTime"];
        cell.detailTypeLabel.text = dict[@"typeText"];
                if([dict[@"divisionText"] isEqualToString:@"请选择"]) {
                    cell.baseDetailTypeLabel.text = @"无";
                }else {
                    cell.baseDetailTypeLabel.text = dict[@"divisionText"];
                }
                if([dict[@"textViewText"] isEqualToString:@"请输入病症描述"]) {
                    
                    cell.textViewLabel.text = @"";
                }else {
                    cell.textViewLabel.text =dict[@"textViewText"];
                }

        
        cell.photoView.image = dict[@"photoImage"];
                return cell;
            }
    }

//}

#pragma mark
#pragma mark -代理方法
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 180;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
@end
